
{{include './header.html'}}
<style>
  img{
    width: 150px;
    height: 150px;
  }
</style>
  <div class="container">
        <form action="" class="form-horizontal book" enctype="multipart/form-data" data-id="{{data?data.id:''}}">
          <div class="form-group">
            <label for="" class="col-sm-2 control-label">书名：</label>
            <div class="col-sm-9">
              <input type="text" name="bookname" id="" class="form-control name" value="{{data?data.bookname:''}}" {{data?'readonly':''}}>
            </div>
          </div>
          <div class="form-group">
            <label for="" class="col-sm-2 control-label">作者：</label>
            <div class="col-sm-9">
              <input type="text" name="author" id="" class="form-control" value="{{data?data.author:''}}">
            </div>
          </div>
          <div class="form-group">
            <label for="" class="col-sm-2 control-label">价格：</label>
            <div class="col-sm-9">
              <input type="text" name="price" id="" class="form-control" value="{{data?data.price:''}}">
            </div>
          </div>
          <div class="form-group">
            <label for="" class="col-sm-2 control-label">折扣（整数）：</label>
            <div class="col-sm-9">
              <input type="text" name="count" id="" class="form-control" value="{{data?data.count:''}}">
            </div>
          </div>
          <div class="form-group">
            <label for="" class="col-sm-2 control-label">销量：</label>
            <div class="col-sm-9">
              <input type="text" name="num" id="" class="form-control" value="{{data?data.num:''}}">
            </div>
          </div>
          <div class="form-group">
            <label for="" class="col-sm-2 control-label">图片：</label>
            <div class="col-sm-9">
               <img src="{{data?'./bookimg/'+data.pic:'./addimg.png'}}" alt="">
               <input type="file" name="pic" style="display: none;" >
            </div>
          </div>
          <div class="form-group" style="text-align: center;">
            <button class="btn btn-success col-sm-9 col-sm-offset-2" >{{data?'修改书籍':'添加书籍'}}</button>
          </div>
        
        </form>
  </div>
  <script>
    $('img').click(function(){
      $(this).next().click()
    })
    $(':file').change(function(){
       var file = this.files[0]
       if(file.type.startsWith('image')){
         var url = URL.createObjectURL(file)
         $('img').attr('src',url)
       }else{
         alert('请选择一张正确的图片')
       }
    })

    $('.book').submit(function(e) {
      e.preventDefault()
      var id = this.dataset.id
      var url =id?'/updatebookData?id='+id:'/addbook?bookname='+$('.name').val()
      console.log(url);
      $.ajax({
        url:url,
        method:'post',
        data:new FormData(this),
        processData:false,
        contentType:false
      }).done(data=>{
         $('#showinfo').modal('show').find('.modal-body').text(data.msg)
           $('#showinfo').on('hide.bs.modal', function () {
          if (data.code == 1) {
            location.href='marangebook'
          } 
        })
      })
    })
  </script>
</body>
</html>